cmake_minimum_required(VERSION 2.8.2)
project(ProjectName)
set(CMAKE_BUILD_TYPE debug)
MESSAGE( STATUS "变量:CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
add_compile_options(-std=c++11)

macro(SUBDIRLIST result curdir)
    file(GLOB children RELATIVE ${curdir} ${curdir}/*)
    set(dirlist "")
    foreach(child ${children})
        if(IS_DIRECTORY ${curdir}/${child})
            LIST(APPEND dirlist ${child})
        endif()
    endforeach()
    set(${result} ${dirlist})
endmacro()

function(SUBFILELIST result curdir pattern)
    file(GLOB children RELATIVE ${curdir} ${curdir}/*)
    set(filelist "")
    foreach(child ${children})
        if(NOT IS_DIRECTORY ${curdir}/${child} AND child MATCHES "${pattern}")
            STRING(REGEX REPLACE ".cpp$" "" child_h ${child})
            LIST(APPEND filelist ${child_h})
        endif()
    endforeach()
    set(${result} ${filelist} PARENT_SCOPE)
endfunction()

include_directories(${CMAKE_SOURCE_DIR}/app/src/main/headers)
SUBDIRLIST(SubFolderMatch ${CMAKE_SOURCE_DIR}/app/src/main/cpp)
foreach(ITEM_FOLDER ${SubFolderMatch})
    message("项目：= ${ITEM_FOLDER}")
    SUBFILELIST(SubFilesMatch ${CMAKE_SOURCE_DIR}/app/src/main/cpp/${ITEM_FOLDER} "solution[_0-9]+.cpp")
    foreach(SOLUTION ${SubFilesMatch})
        if(EXISTS ${CMAKE_SOURCE_DIR}/app/src/main/cpp/${ITEM_FOLDER}/app.cpp)
            set(TARGET "${ITEM_FOLDER}_${SOLUTION}")
            message("方案：= ${TARGET}")
            add_executable(${TARGET} ${CMAKE_SOURCE_DIR}/app/src/main/cpp/${ITEM_FOLDER}/app.cpp)
            add_library(${TARGET}_lib ${CMAKE_SOURCE_DIR}/app/src/main/cpp/${ITEM_FOLDER}/${SOLUTION}.cpp)
            target_link_libraries(${TARGET} ${TARGET}_lib)
        endif()
    endforeach()
endforeach()

# include_directories(${CMAKE_SOURCE_DIR}/app/src/main/headers)
# add_executable(exe1 ${CMAKE_SOURCE_DIR}/app/src/main/cpp/app.cpp)
# add_executable(exe2 ${CMAKE_SOURCE_DIR}/app/src/main/cpp/app.cpp)
# add_library(lib1  ${CMAKE_SOURCE_DIR}/app/src/main/cpp/solution_1.cpp)
# add_library(lib2  ${CMAKE_SOURCE_DIR}/app/src/main/cpp/solution_1.cpp)
# target_link_libraries(exe1 lib1)
# target_link_libraries(exe2 lib2)


